﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// WMS_客检规则卡控
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSCustomSQC_CheckController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public WMSCustomSQC_CheckController(ILogger<WMSCustomSQC_CheckRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<WMSCustomSQC_CheckController>();

        }

        /// <summary>
        /// WMS_客检规则卡控
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostCustomSQC_Check([FromBody] WMSCustomSQC_CheckRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSCustomSQC_CheckResponseDto re = new WMSCustomSQC_CheckResponseDto();
            string order_no = strparm.order_no;
            string create_time = strparm.order_no;


            if (order_no.Trim() == "")
            {
                strMsg = "order_no入库单号不能为空，请检查后再试！";
            }

            if (create_time.Trim() == "")
            {
                strMsg = "create_time创建日期不能为空，请检查后再试！";
            }



            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    var row = strparm.rows;
                    foreach (var item in row)
                    {
                        string sysid = "";
                        string TRACEPREFIX = "";

                        if (item.barcode_no.Contains("GKAG")|| item.barcode_no.Contains("ZT") || item.barcode_no.Contains("230972-"))
                        {
                            if (item.barcode_no.Contains("GKAG"))
                            {
                                TRACEPREFIX = "GKAG";
                            }
                            else if(item.barcode_no.Contains("ZT"))
                            {
                                TRACEPREFIX = "ZT";
                            }
                            else
                            {
                                TRACEPREFIX = "2309";
                            }
                            sysid = "TMS0050";
                        }
                        else
                        {
                            sysid = "TMS0051";
                        }
                        dic = new Dictionary<string, string>();
                        dic.Add("TRACEPREFIX", TRACEPREFIX);
                        dic.Add("WORKNO", item.work_orderno);
                        dic.Add("SKU_END", "1");
                        string  sqlstr= MethodHelp.GetTranSQL(sysid, dic, con);//查询当前客检记录是否存在,MYSQL获取语句
                        DataTable check_ccode = DB.OracleQuery(sqlstr.ToString(),gz_con);//Oracle驱动查询
                        if (check_ccode.Rows.Count <= 0)
                        {
                            strMsg = strMsg + "--条码<" + item.barcode_no + ">对应工单<"+ item.work_orderno + "> 找不到对应的客检规则，请联系计划负责人维护。";
                        }                        
                    }
                  
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
